﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
using QLKH.PopUp.Khachhang;

namespace QLKH.View.Khachhang
{
    public partial class dsKhachhang : UserControl
    {
        DataView view = new DataView();
        public dsKhachhang()
        {
            InitializeComponent();
        }

        int currentPageIndex = 1;
        int pageSize = 23;
        int pageNumber = 1;
        int fistRow, lastRow;
        int rows;

        private void bintreeview()
        {
            //add phường xã và đường phố
            clPX clpx = new clPX();
            DataTable dtpx = clpx.getdata(1,0);
            if (dtpx != null && dtpx.Rows.Count > 0)
            {
                TreeNode rootNode = new TreeNode();
                rootNode.Text = "Danh sách phường xã";
                rootNode.Tag = "D-0-0";
                treeViewData.Nodes.Add(rootNode);
                TreeNode rootItem = null;
                TreeNode nodepho = null;
                for (int i = 0; i < dtpx.Rows.Count; i++)
                {
                    rootItem = new TreeNode();
                    rootItem.Text = dtpx.Rows[i]["ten"].ToString();
                    rootItem.Tag = "D-" + dtpx.Rows[i]["id"].ToString()+"-0";
                    rootNode.Nodes.Add(rootItem);

                    clPho pho = new clPho();
                    DataTable dtpho = pho.getdata(1,Int16.Parse(dtpx.Rows[i]["id"].ToString()));
                    for (int j = 0; j < dtpho.Rows.Count; j++)
                    {
                        nodepho = new TreeNode();
                        nodepho.Text = "Đường " + dtpho.Rows[j]["ten"].ToString();
                        nodepho.Tag = "D-" + dtpx.Rows[i]["id"].ToString() +"-"+dtpho.Rows[j]["id"].ToString();
                        rootItem.Nodes.Add(nodepho);
                    }
                }
            }


            // add duong
            //clDuong cld = new clDuong();
            //DataTable dtduong = cld.getdata(1);
            //if (dtduong != null && dtduong.Rows.Count > 0)
            //{
            //    TreeNode rootNode = new TreeNode();
            //    rootNode.Text = "Danh sách đường";
            //    rootNode.Tag = "D-0";
            //    treeViewData.Nodes.Add(rootNode);
            //    TreeNode rootItem = null;
            //    for (int i = 0; i < dtduong.Rows.Count; i++)
            //    {
            //        rootItem = new TreeNode();
            //        rootItem.Text = dtduong.Rows[i]["tenDuong"].ToString();
            //        rootItem.Tag = "D-" + dtduong.Rows[i]["id"].ToString();
            //        rootNode.Nodes.Add(rootItem);
            //    }
            //}
            // dong ho tong
            clDHTong cldht = new clDHTong();
            DataTable dtdht = cldht.getdata(1);
            if (dtdht != null && dtdht.Rows.Count > 0)
            {
                TreeNode rootNode = new TreeNode();
                rootNode.Text = "Đồng hồ tổng";
                rootNode.Tag = "DHT-0-0";
                treeViewData.Nodes.Add(rootNode);
                TreeNode rootItem = null;
                for (int i = 0; i < dtdht.Rows.Count; i++)
                {
                    rootItem = new TreeNode();
                    rootItem.Text = dtdht.Rows[i]["tenDHTong"].ToString();
                    rootItem.Tag = "DHT-" + dtdht.Rows[i]["id"].ToString()+"-0";
                    rootNode.Nodes.Add(rootItem);
                }
            }
            // khoi co quan

        }

        void pagetotal()
        {
            string value = string.Empty;
            TreeNode nod = treeViewData.SelectedNode;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            if (value.Length > 0)
            {
                clKhachhang clsd = new clKhachhang();
                rows = clsd.countrowkh(value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            pageNumber = rows % pageSize != 0 ? rows / pageSize + 1 : rows / pageSize;
            lbtotalpage.Text = "/" + pageNumber.ToString();
            cbbpage.Items.Clear();
            for (int i = 1; i <= pageNumber; i++)
            {
                cbbpage.Items.Add(i + "");
            }
            //cbbpage.SelectedIndex = 0;
        }

        private void dsKhachhang_Load(object sender, EventArgs e)
        {
            bintreeview();

            TreeNode nod = treeViewData.Nodes[0];
            treeViewData.SelectedNode = nod;
            pagetotal();

            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            string value = string.Empty;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
        }

        private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            lbrowindex.Text = e.RowIndex.ToString();
        }

        //public void Search(string id, string type)
        //{   // khai báo 1 hàm với 2 tham số đầu vào là str1, và str2 nó sẽ đưa dữ liệu vào 2 lable
        //    // bin du lieu
        //    //MessageBox.Show(txtsearch + "-" + idd);
        //    string querySearch = "1=1";
        //    if (type == "D")
        //    {
        //        //querySearch += " and idDHTong = 0";
        //        if (Int16.Parse(id) > 0)
        //            querySearch += " and idDuong = " + id;
        //        else
        //            querySearch += " and idDuong > 0";
        //    }
        //    else
        //    {
        //        if (Int16.Parse(id) > 0)
        //            querySearch += " and idDHTong = " + id;
        //        else
        //            querySearch += " and idDHTong > 0";
        //    }
        //    view.RowFilter = querySearch;
        //    lbrowindex.Text = "-1";
        //}

        //public void Search(string id, string id2, string type)
        //{   // khai báo 1 hàm với 2 tham số đầu vào là str1, và str2 nó sẽ đưa dữ liệu vào 2 lable
        //    // bin du lieu
        //    //MessageBox.Show(txtsearch + "-" + idd);
        //    string querySearch = "1=1";

        //    if (type == "D")
        //    {
        //        //querySearch += " and idDHTong = 0";
        //        if (Int16.Parse(id) > 0)
        //        {
        //            querySearch += " and idPX = " + id;
        //            if (Int16.Parse(id2) > 0)
        //                querySearch += " and idPho = " + id2;
        //            else
        //                querySearch += " and idPho > 0";
        //        }
        //        else
        //            querySearch += " and idPX > 0";
        //    }
        //    else
        //    {
        //        if (Int16.Parse(id) > 0)
        //        {
        //            querySearch += " and idDHTong = " + id;
        //            if (Int16.Parse(id2) > 0)
        //                querySearch += "";
        //        }
        //        else
        //            querySearch += " and idDHTong > 0";
        //    }
        //    view.RowFilter = querySearch;
        //    lbrowindex.Text = "-1";
        //}

        private void treeViewData_AfterSelect(object sender, TreeViewEventArgs e)
        {
            cbbpage.Text = "1";
            pagetotal();
            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            string value = string.Empty;
            value = e.Node.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
            //string value = string.Empty;
            //value = e.Node.Tag.ToString().Trim();
            //lbsearch.Text = value;
            //if (value.Length > 0)
            //{
            //    Search(value.Split('-')[1].Trim(), value.Split('-')[2].Trim(),value.Split('-')[0].Trim());
            //}
        }

        public void EditCus(int id)
        {   // khai báo 1 hàm với 2 tham số đầu vào là str1, và str2 nó sẽ đưa dữ liệu vào 2 lable
            // bin du lieu
            //clKhachhang clkh = new clKhachhang();
            //DataTable dtkh = clkh.getdata(3);

            //dataGrid.AutoGenerateColumns = false;
            //dataGrid.DataSource = view = dtkh.DefaultView;
            //pagetotal();

            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            TreeNode nod = treeViewData.SelectedNode;
            string value = string.Empty;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
        }

        private void tool_capnhat_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int64 id = Int64.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());
                popCapnhat usfritem = new popCapnhat(id);
                // ké thằng GetValue của form1 vào form2
                usfritem.MyGetData = new popCapnhat.EditCus(EditCus);
                usfritem.ShowDialog();
            }
            else
                MessageBox.Show("Bạn chưa chọn khách hàng.");
        }

        public void ThaycumDH(int id)
        {
            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            TreeNode nod = treeViewData.SelectedNode;
            string value = string.Empty;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
            //clKhachhang clkh = new clKhachhang();
            //DataTable dtkh = clkh.getdata(3);

            //dataGrid.AutoGenerateColumns = false;
            //dataGrid.DataSource = view = dtkh.DefaultView;
        }

        private void tool_thaycumdh_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int64 id = Int64.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());
                Int16 idd = Int16.Parse(dataGrid.Rows[row].Cells["idDuong"].Value.ToString());
                popThaycumdongho usfritem = new popThaycumdongho(id);
                // ké thằng GetValue của form1 vào form2
                usfritem.MyGetData = new popThaycumdongho.ThaycumDH(ThaycumDH);
                usfritem.ShowDialog();
            }
            else
                MessageBox.Show("Bạn chưa chọn khách hàng.");
        }

        public void LapDH(int id)
        {
            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            TreeNode nod = treeViewData.SelectedNode;
            string value = string.Empty;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
            //clKhachhang clkh = new clKhachhang();
            //DataTable dtkh = clkh.getdata(3);

            //dataGrid.AutoGenerateColumns = false;
            //dataGrid.DataSource = view = dtkh.DefaultView;
        }

        private void tool_lapdongho_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int64 id = Int64.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());
                popLapdongho usfritem = new popLapdongho(id);
                // ké thằng GetValue của form1 vào form2
                usfritem.MyGetData = new popLapdongho.LapDH(LapDH);
                usfritem.ShowDialog();
            }
            else
                MessageBox.Show("Bạn chưa chọn khách hàng.");
        }

        public void KhoansKH(int id)
        {
            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            TreeNode nod = treeViewData.SelectedNode;
            string value = string.Empty;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
            //clKhachhang clkh = new clKhachhang();
            //DataTable dtkh = clkh.getdata(3);

            //dataGrid.AutoGenerateColumns = false;
            //dataGrid.DataSource = view = dtkh.DefaultView;
        }

        private void tool_dungkhoan_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int64 id = Int64.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());
                popKhoan usfritem = new popKhoan(id);
                // ké thằng GetValue của form1 vào form2
                usfritem.MyGetData = new popKhoan.KhoanCus(KhoansKH);
                usfritem.ShowDialog();
            }
            else
                MessageBox.Show("Bạn chưa chọn khách hàng.");
        }

        public void Xuly(int id)
        {
            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            TreeNode nod = treeViewData.SelectedNode;
            string value = string.Empty;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
            //clKhachhang clkh = new clKhachhang();
            //DataTable dtkh = clkh.getdata(3);

            //dataGrid.AutoGenerateColumns = false;
            //dataGrid.DataSource = view = dtkh.DefaultView;
        }

        private void tool_xuly_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int64 id = Int64.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());
                popXuly usfritem = new popXuly(id);
                // ké thằng GetValue của form1 vào form2
                usfritem.MyGetData = new popXuly.AddXuLy(Xuly);
                usfritem.ShowDialog();
            }
            else
                MessageBox.Show("Bạn chưa chọn khách hàng.");
        }
        
        public void TimKiem(string txtsearch,string type, Int16 id)
        {   
            string querySearch = "1=1";
            if (txtsearch.Length > 0)
                querySearch += " and (maCu LIKE '%" + txtsearch + "%')";
            if (id > 0)
            {
                if (type == "D")
                    querySearch += " and idDuong = " + id + " and idDHTong = 0";
                else
                    querySearch += " and idDHTong = " + id;
            }
            view.RowFilter = querySearch;
        }
        // tim kiếm
        private void tool_search_Click(object sender, EventArgs e)
        {
            popTimkiem usfritem = new popTimkiem();
            // ké thằng GetValue của form1 vào form2
            usfritem.MyGetData = new popTimkiem.TimKiem(TimKiem);
            usfritem.ShowDialog();
        }

        private void tool_lichsukh_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Chức năng đang được cập nhật.");
        }

        private void nuberricPagesize_ValueChanged(object sender, EventArgs e)
        {
            pageSize = Convert.ToInt32(nuberricPagesize.Value);
            pagetotal();
            pageNumber = int.Parse(cbbpage.Text);

            TreeNode nod = treeViewData.SelectedNode;
            string value = string.Empty;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
        }

        private void cbbpage_SelectedIndexChanged(object sender, EventArgs e)
        {
            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            TreeNode nod = treeViewData.SelectedNode;
            string value = string.Empty;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
        }

        public void capnhatvitri(int id)
        {
            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            TreeNode nod = treeViewData.SelectedNode;
            string value = string.Empty;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
        }

        private void updatevitri_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int64 id = Int64.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());
                popUpdatViTri usfritem = new popUpdatViTri(id);
                // ké thằng GetValue của form1 vào form2
                usfritem.MyGetData = new popUpdatViTri.VitriCus(capnhatvitri);
                usfritem.ShowDialog();
            }
            else
                MessageBox.Show("Bạn chưa chọn khách hàng.");
        }

        public void chuyenvitri(int id)
        {
            pageNumber = int.Parse(cbbpage.Text);
            pageSize = int.Parse(nuberricPagesize.Value.ToString());

            TreeNode nod = treeViewData.SelectedNode;
            string value = string.Empty;
            value = nod.Tag.ToString().Trim();
            lbsearch.Text = value;
            DataTable dtkh = new DataTable();
            if (value.Length > 0)
            {
                clKhachhang clkh = new clKhachhang();
                dtkh = clkh.loadkh(pageNumber, pageSize, value.Split('-')[0].Trim(), Int16.Parse(value.Split('-')[1].Trim()), Int16.Parse(value.Split('-')[2].Trim()));
            }
            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
        }

        private void tool_chuyenvitri_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int64 id = Int64.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());
                popChuyenViTri usfritem = new popChuyenViTri(id);
                // ké thằng GetValue của form1 vào form2
                usfritem.MyGetData = new popChuyenViTri.VitriCus(chuyenvitri);
                usfritem.ShowDialog();
            }
            else
                MessageBox.Show("Bạn chưa chọn khách hàng.");
        }
    }
}
